package com.glory.brepda.activity.lanjian;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class SOPEqpClearActivity  extends BaseActivity {

    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBCOPType)
    TextView tvBCOPType;
    @BindView(R.id.ivBCOPArrow)
    ImageView ivBCOPArrow;
    @BindView(R.id.flBCOPList)
    FrameLayout flBCOPList;
    @BindView(R.id.etXM)
    FreeUI_ClearEditText etXM;
    @BindView(R.id.etStepName)
    FreeUI_ClearEditText etStepName;
    @BindView(R.id.etUserName)
    FreeUI_ClearEditText etUserName;
    @BindView(R.id.etEqp)
    FreeUI_ClearEditText etEqp;
    @BindView(R.id.etbeizu)
    FreeUI_ClearEditText etbeizu;

    private FreeUI_GeneralPop codePop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_sopeqp_clear;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);

        // ①组件监听的方式
        flBCOPList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flBCOPList.getWidth(), new ArrayList<String>(Arrays.asList("已完成","未完成")), ivBCOPArrow, (ty, i) -> {
                        tvBCOPType.setText(ty);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivBCOPArrow);
                codePop.showAsDropDown(flBCOPList, 0, 0);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {

                if(StringUtils.isEmpty(getText(etUserName)) || StringUtils.isEmpty(getText(etEqp)) || StringUtils.isEmpty(tvBCOPType.getText().toString()) ){
                    showToast("必填项不能为空",false);
                    return;
                }
                if(tvBCOPType.getText().toString().equals("未完成") && StringUtils.isEmpty(getText(etbeizu))){
                    showToast("选择未完成则必须填写备注",false);
                    return;
                }

                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {

                            etUserName.setText(null);
                            etEqp.setText(null);
                            tvBCOPType.setText(null);
                            etbeizu.setText(null);
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        loadError(response.getException(), "执行异常");

                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTION", "SAVE");
                map.addProperty("ETXM", getText(etXM));
                map.addProperty("ETSTEPNAME", getText(etStepName));
                map.addProperty("ETUSERNAME", getText(etUserName));
                map.addProperty("ETEQP", getText(etEqp));
                map.addProperty("TVRESULT", tvBCOPType.getText().toString());  //结果
                map.addProperty("ETBZ", getText(etbeizu));  //备注

                NetUtils.requestPostNet("IF.SOP_EQP_CLEAR", "Common", SOPEqpClearActivity.this, map, callBack);

    }


}